package com.gzs.learn.payment.algorithm;

import com.gzs.learn.inf.split.SplitIdUtil;

/**
 * DB分片算法
 */
public class DBShardingAlgorithm extends BaseShardingAlgorithm {
    private static final int PAYMENT_DB_COUNT = SplitIdUtil.PAYMENT_DB_SPLIT;
    private static final int PAYMENT_DB_SHIFT = PAYMENT_DB_COUNT - 1;
    private static final int DB_SHIFT = 0b11111000;
    private static final int DB_BITS = 5;

    @Override
    long getActualTarget(long shardingTarget) {
        long dbKey = (shardingTarget & DB_SHIFT) >> (SplitIdUtil.SPLIT_BIT - DB_BITS);
        // 分库键取模
        return dbKey & PAYMENT_DB_SHIFT;
    }
}
